/**
 * 制作:  叶风
 * Qq:    355007778
 * Email: ye.fn@163.com 
 * 
 */
  
var ROOT_URL = '', JS_URL = 'js/',TEMP = {};

$.ajaxSetup({cache:false});

var language ={
	data:[],
	load:function(a){
		for(var i in a)
			this.data[i] = a[i];
	},
    
    //转换
	_:function(str){
		if( typeof(this.data[str])=='string' )
			return this.data[str];
		return str;
	},
    
    //格式化
	_f:function(str, s){
		var arg = [s];
		str = this._(str);
		for(var i=0,l=arg.length; i<l; i++)
			str = str.replace('%s'+i,  arg[i]);
		return str;
	}
}

var $_obj = function(obj){	return typeof(obj)=="object"? obj : document.getElementById(obj);}

// 合并变量
function mergeConf(opt, opt2)
{
    var i;
    for(i in opt2)
        opt[i] = opt2[i];
    return opt;
}

// 加载事件方法
function onEvent(obj, e, fun)
{
	if (obj.addEventListener)
		obj.addEventListener(e, fun, false);
	else
		obj.attachEvent('on'+ e, fun);
}

//
var setCookie = function(name, val, expiredays)
{
    var t = '';
	if (expiredays){
		var exdate = new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		t = ";expires="+exdate.toGMTString();
	 }
	document.cookie = name + "=" + escape(val) + t;
}
var getCookie = function(name)
{
  var arrCookie = document.cookie.split("; ");
  for (var i=0; i < arrCookie.length; i++)
  {
    var arr = arrCookie[i].split("=");
    if (name == arr[0])
      return unescape(arr[1]);
  }
  return null;
}

//  ajax
function ajaxUpDiv(obj, url, data, f)
{	
	var ob = $(obj);
	var rf = function(r){
		ob.html(r);
		if(f)f(r);
	};
	
	setLoading(ob[0]);
	if(data)
		$.post(url, data, rf);
	else
		$.get(url, '', rf);
}

function checAjaxRe(res, autoC)
{
	var str;
	if (!res || typeof(res.status)=='undefined')
		str = res;
	else if(res.status===true)
		return true;
	else{
		str = res.status;
		if (typeof autoC!='number')autoC=2;
	}
	
	var a= box(str, language._('you qing ti shi'));
	if (typeof autoC=='number' && autoC>0)
		a.close(autoC);

}
function setLoading(o){
	var obj=$_obj(o),str=obj.innerHTML,ah=document.createElement('span'),a={};
	obj.innerHTML = '';	
	obj.appendChild(ah);
	ah.className = 'ajax-loading';
	a.restore = function(){obj.innerHTML = str;};
	return a;
}

//  打开窗口
function w_open(url, name, width, face){	
	if (!face)
		face =  'toolbar=0,status=0,resizable=1,location=1,scrollbars=1';
	if (typeof(width)=='undefined')
		face += ',width=670';
	else if (width>0)
		face += ',width='+width;
	var w = window.open(url, name, face);
	w.focus();
	return w;
}
var w_openMD = function(url, face)
{	if(face==null)
		face =  'dialogWidth=500px;';
	window.showModalDialog(url, window, face);
}

//  移动
var moveDIV = function (e){	
    with(this){
        if (!e)e =event ; //FOR IE
        var l = e.clientX -offsetLeft, t = e.clientY -offsetTop;
        function _move(e){
                with(this){
                    if (!e) e =event;  //FOR IE
                    style.left = e.clientX -l +"px";
                    style.top = e.clientY -t +"px";
                }
            };
        function un_move(){
            $(document).unbind("mousemove", _move).unbind('mouseup',un_move);
        }
        $(document).bind("mousemove", _move).bind('mouseup',un_move);
    }
}

/**
 * 创建选项列表
 * @param {type} id
 * @param {type} arr 
 * @param {type} selValue
 * @param {type} startLength
 * @returns {undefined}
 */
var setSelectOption = function(id, arr, selValue, startLength)
{
    var select_obj = $_obj(id) , j = startLength||0,  i;
	select_obj.length 	= j;
	for(i=0; i<arr.length; i++,j++){	
        var opt = arr[i], s = (selValue==opt.v ?  true : false);
        select_obj[j] = new Option(opt.n, opt.v, s, s);
    }
}
var getSelectText = function(id)
{	return  $_obj(id).options[$_obj(id).selectedIndex].text;}
var getSelectValue = function(id)
{	return  $_obj(id).options[$_obj(id).selectedIndex].value;}


// 过虚 js
var strip_script = function(s) {
	return s.replace(/<script.*?>.*?<\/script>/ig, '');
}
var loadScript = function(file)
{	
	var js = document.createElement("script");
	js.type = "text/javascript";
    js.src = JS_URL + file;
    document.getElementsByTagName("body")[0].appendChild(js);
}
// js文件路径
var scriptPath = function(n)
{
    var es = document.getElementsByTagName("script");
	var p;
    for (var i = 0; i < es.length; i++)
     {  
	 	if(es[i].src)
	 	 {	p = es[i].src.lastIndexOf('/')+1;
	 		if ( es[i].src.substring(p)==n)
            	return es[i].src.substring(0,p);
		 }
	 }
}

// 交换DIV
function switch_div(obj, id)
{		
    if ($('#'+id).length<1)
        return false;
    
	var s_id = 'switchD_'+ s;
	if (TEMP[s_id])  //还原
		$('#'+TEMP[s_id]).append( $('#switch' +obj).children() );  
	TEMP[sid] = id;
	$('#switch' + obj ).append( $('#'+id).children() );	
}

// 初始化 iframe
var set_iframe = function(id, opt, intv)
{	
	var obj = $_obj(id);
	if(!intv)
		intv =0;
	if( opt!='x') obj.height = (obj.Document ? obj.Document.documentElement.scrollHeight : obj.contentDocument.documentElement.offsetHeight) + intv;
	if( opt!='y') obj.width = obj.Document ? obj.Document.documentElement.scrollWidth : obj.contentDocument.documentElement.offsetWidth;
}

// 文本宽度
var textWidth = function(text){ 
        var sensor = $('<pre>'+ text +'</pre>').css({display: 'none'}); 
        $('body').append(sensor); 
        var width = sensor.width();
        sensor.remove(); 
        return width;
};


function htmlEncode(s)
{
    var t = document.createElement('div');
    t.textContent===null ? t.innerText=s : t.textContent=s;
    s = t.innerHTML;
    t = null;
    return s;
}
function htmlEncode_q(s)
{
    return s.replace('"','&quot;');
}